package com.ch999.detect.View.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ch999.detect.R;
import com.ch999.detect.mode.bean.GoodsBjgz;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 检测项adapter
 * Created by ch999android on 2017/12/26.
 */

public class GoodsBjgzAdapter extends RecyclerView.Adapter<GoodsBjgzAdapter.MyViewholder> {

    private List<GoodsBjgz.AttrBean> attrBeanList;
    private Context context;
    private ItemClickListner itemClickListner;

    public GoodsBjgzAdapter(List<GoodsBjgz.AttrBean> attrBeanList, Context context) {
        this.attrBeanList = attrBeanList;
        this.context = context;
    }

    public void updateData(List<GoodsBjgz.AttrBean> attrBeanList) {
        this.attrBeanList = attrBeanList;
        notifyDataSetChanged();
    }



    public void setItemClickListner(ItemClickListner itemClickListner) {
        this.itemClickListner = itemClickListner;
    }

    @Override
    public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewholder(LayoutInflater.from(context).inflate(R.layout.item_goodsbjgz, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewholder holder, int position) {
        GoodsBjgz.AttrBean attrBean = attrBeanList.get(position);
        holder.tvName.setText(attrBean.getTpname());

        if (!TextUtils.isEmpty(attrBean.getIcon())) {
            Picasso.with(context).load(attrBean.getIcon()).into(holder.icon);
        }
        if (attrBean.getItemPosation() == -1) {

            holder.tvStats.setText("未检测");
            holder.tvStats.setTextColor(context.getResources().getColor(R.color.es_gr));

        } else {
            String name = attrBean.getValues().get(attrBean.getItemPosation()).getName();
            holder.tvStats.setText(name);

            if (name.contains("不正常")) {

                holder.tvStats.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
            } else {
                holder.tvStats.setTextColor(context.getResources().getColor(R.color.es_b));
            }


        }
    }


    @Override
    public int getItemCount() {
        return attrBeanList.size();
    }

    class MyViewholder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvStats;

        ImageView icon;

        public MyViewholder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvStats = (TextView) itemView.findViewById(R.id.tvStats);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListner.onClick(getAdapterPosition());
                }
            });
        }
    }

    public interface ItemClickListner{
        void onClick(int position);
    }
}
